const mongoose = require('mongoose');
const favoriteNameSchema = new mongoose.Schema({
  name: {
    type: String,
    required: [true, 'Please provide a name for the favorite'],
    trim: true,
  },
  user: {
    type: mongoose.Schema.ObjectId,
    ref: 'User',
    required: [true, 'Favorite must belong to a user'],
  },
  videos: [
    {
      type: mongoose.Schema.ObjectId,
      ref: 'Video',
      default: [],
    },
  ],
});
// favoriteNameSchema.virtual('videos', {
//   ref: 'Video',
//   foreignField: 'favoriteNames',
//   localField: '_id',
// });
favoriteNameSchema.index({ name: 1, user: 1, video: 1 }, { unique: true });

favoriteNameSchema.pre(/^find/, function (next) {
  this.populate({
    path: 'user',
    select: 'name photo',
  });
  this.populate({
    path: 'videos',
    select: 'name footer url',
  });
  next();
  // this.populate({
  //   path: 'videos',
  //   select: 'name url',
  // });
});
const FavoriteName = mongoose.model('FavoriteName', favoriteNameSchema);
module.exports = FavoriteName;
